/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.pacs.tools;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author 52461
 */
public class FileUtil {

    public static String outWriter(String name) {
        String huge = "";
        try {
            InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(name), "UTF-8");
            BufferedReader in = new BufferedReader(inputStreamReader);

            for (;;) {
                String data = in.readLine();
//                if (!data.contains(eol)){
//                    data+=eol;
//                }
                if (data == null) {
                    break;
                } else {
                    huge += data;
                }
            }
            in.close();
            inputStreamReader.close();
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        }
        return huge;
    }
    
    public static String outWriter(String name,String ucode) {
        String huge = "";
        try {
            InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(name), ucode);
            BufferedReader in = new BufferedReader(inputStreamReader);

            for (;;) {
                String data = in.readLine();
//                if (!data.contains(eol)){
//                    data+=eol;
//                }
                if (data == null) {
                    break;
                } else {
                    huge += data;
                }
            }
            in.close();
            inputStreamReader.close();
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        }
        return huge;
    }

    public static List<File> getFile(String path) {
        // System.out.println(path);
        List<File> list = new ArrayList<File>();
        File f = new File(path);
        //显示目录的方法

        //判断传入对象是否为一个文件夹对象
        if (!f.isDirectory()) {
            System.out.println("你输入的不是一个文件夹，请检查路径是否有误！！");
        } else {
            File[] t = f.listFiles();
            for (int i = 0; i < t.length; i++) {
                //判断文件列表中的对象是否为文件夹对象，如果是则执行tree递归，直到把此文件夹中所有文件输出为止
                if (t[i].isDirectory()) {
                    // System.out.println(t[i].getName()+"\tttdir");
                    list.addAll(getFile(t[i].getAbsolutePath()));
                } else {
                    list.add(t[i]);
                }
            }
        }
        return list;

    }

    public static File[] getAllFile(String path) {
        System.out.println("--------------------------");
        List<File> list = getFile(path);
        File[] files = new File[list.size()];
        for (int i = 0; i < list.size(); i++) {
            files[i] = list.get(i);
        }
        return files;
        // System.out.println(list.size());
    }
    public static void writeText(String name, String Content) {
        try {
            /*
             * FileWriter outFile=new FileWriter(name); PrintWriter ox=new
             * PrintWriter(outFile); ox.print(Content); ox.close();
             * outFile.close();
             */
            OutputStreamWriter outputStream = new OutputStreamWriter(new FileOutputStream(name), "UTF-8");
            //outputStream.write("I am here");
            outputStream.write(Content);
            outputStream.close();
        } catch (IOException ex) {
        }
    }

}
